package cn.tansanqinger.service.chatroom.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.chatroom.ChatMessageSqlDaoImpl;
import cn.tansanqinger.dao.sql.chatroom.FriendlistSqlDaoImpl;
import cn.tansanqinger.dao.sql.chatroom.GroupSqlDaoImpl;
import cn.tansanqinger.dao.sql.user.UserMessageDispose;
import cn.tansanqinger.pojo.Friendlist;
import cn.tansanqinger.pojo.Chatmessage;
import cn.tansanqinger.pojo.Flick;
import cn.tansanqinger.pojo.User;
import cn.tansanqinger.service.chatroom.IFriendlistService;
import cn.tansanqinger.util.PageData;
import cn.tansanqinger.util.Tools;

@Service
public class FriendlistServiceImpl implements IFriendlistService{
	
	@Autowired
	private FriendlistSqlDaoImpl impl;
	@Autowired
	private UserMessageDispose userDispose;
	@Autowired
	private GroupSqlDaoImpl gimpl;
	@Autowired
	private ChatMessageSqlDaoImpl cimpl;

	@Override
	public List<User> selectMessage(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		String account = user.getAccount();
		List<Friendlist> friendlists = impl.selectMessage(user.getId());
		if(!friendlists.isEmpty()) {
			List<User> users = new ArrayList<User>(friendlists.size());
			for (int i = 0; i < friendlists.size(); i++) {
				user = userDispose.selectOneId(friendlists.get(i).getIid());
				if(!"".equals(Tools.isEmpty(friendlists.get(i).getFcomment()))) {
					user.setComment(friendlists.get(i).getFcomment());
				} else {
					user.setComment(user.getAccount());
				}
				Chatmessage chatmessage = cimpl.selectChatmessagesOne(user.getAccount(), account);
				if(chatmessage!=null) {
					user.setPasswd(chatmessage.getMessage());
				} else {
					user.setPasswd("");
				}
				users.add(user);
			}
			return users;
		}
		return null;
	}

	@Override
	public List<Friendlist> selectAccountMessage(String account) {
		return impl.selectAccountMessage(account);
	}

	@Override
	public PageData add(HttpServletRequest request) {
		PageData pd = new PageData(request);
		Friendlist friendlist = new Friendlist();
		User user = (User) request.getSession().getAttribute("user");
		friendlist.setUid(user.getId());
		friendlist.setIid(Integer.parseInt(pd.get("iid")+""));
		friendlist.setFcomment(pd.get("comment")+"");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		friendlist.setFtime(System.currentTimeMillis()/1000);
		friendlist.setFdata(format.format(new Date()));
		pd = new PageData();
		if(1==impl.add(friendlist)) {
			pd.put("code", "0");
			pd.put("msg", "添加成功");
		} else {
			pd.put("code", "-1");
			pd.put("msg", "添加失败");
		}
		return pd;
	}

	@Override
	public Friendlist selectIdOne(String id) {
		return impl.selectOnt(Integer.parseInt(id));
	}

	@Override
	public PageData delete(String id) {
		PageData pd = new PageData();
		if(1==impl.deleteOne(Integer.parseInt(id))) {
			pd.put("code", "0");
			pd.put("msg", "添加成功");
		} else {
			pd.put("code", "-1");
			pd.put("msg", "添加失败");
		}
		return pd;
	}

	@Override
	public PageData modification(HttpServletRequest request) {
		PageData pd = new PageData(request);
		Friendlist friendlist = selectIdOne(pd.get("id")+"");
		if(friendlist!=null) {
			friendlist.setFcomment(pd.getString("comment"));
			pd = new PageData();
			if(1==impl.modification(friendlist)) {
				pd.put("code", "0");
				pd.put("msg", "修改成功");
			} else {
				pd.put("code", "-1");
				pd.put("msg", "修改失败");
			}
		}
		return pd;
	}


	@Override
	public PageData addNewLinkmanAndGroupChat(HttpServletRequest request) {
		PageData pd = new PageData();
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")&&(id.length()==8||id.length()==11)) {
			if(id.length()==11) {//查询用户
				User user = userDispose.selectOnePhone(id);
				if(user!=null) {
					pd.put("code", "0");
					pd.put("msg","查找成功");
					pd.put("id", user.getId());
					pd.put("phone", user.getPhone());
					pd.put("name", user.getName());
					pd.put("age", user.getAge());
					pd.put("sex", user.getSex());
					pd.put("brithday", user.getBrithday());
					pd.put("email", user.getEmail());
					pd.put("jurisdiction", user.getJurisdiction());
					pd.put("school", user.getSchool());
					pd.put("grade", user.getGrade());
					pd.put("img", user.getImg());
					pd.put("perSignature", user.getPersignature());
					pd.put("userandgroup",1);//用户
					return pd;
				}
			} else { //查询群聊
				Flick group = gimpl.selectGroupOne(Integer.parseInt(id));
				if(group!=null) {
					pd.put("userandgroup",0);//群聊
					pd.put("code", "0");
					pd.put("msg","查找成功");
					pd.put("id",group.getGid());
					pd.put("phone", group.getGsn());
					pd.put("name", group.getGname());
					pd.put("gdata", group.getGdata());
					pd.put("gimg", group.getGimg());
					pd.put("guid", group.getGuid());
					pd.put("guname", userDispose.selectOneId(group.getGuid()).getName());
					pd.put("gnumber", group.getGnumber());
					pd.put("jurisdiction", group.getJurisdiction());
					pd.put("guse", group.getGuse());
					return pd;
				}
			}
		} 
		pd.put("code", "-1");
		pd.put("msg", "查找失败");
		return pd;
	}


	@Override
	public PageData deleteFriend(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		String id = Tools.isEmpty(request.getParameter("id"));
		PageData pd = new PageData();
		if(id.matches("\\d+")) {
			if(impl.deleteFriend(Integer.parseInt(id), user.getId())==1) {
				pd.put("code", "0");
				pd.put("msg", "删除成功");
			} else {
				pd.put("code", "-1");
				pd.put("msg", "删除失败");
			}
		}
		return null;
	}

	@Override
	public PageData selectUser(HttpServletRequest request) {
		PageData pd = new PageData();
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")) {
			User user = userDispose.selectOneId(Integer.parseInt(id));
			if(user!=null) {
				pd.put("code", "0");
				pd.put("msg","查找成功");
				pd.put("id", user.getId());
				pd.put("phone", user.getPhone());
				pd.put("name", user.getName());
				pd.put("age", user.getAge());
				pd.put("sex", user.getSex());
				pd.put("brithday", user.getBrithday());
				pd.put("email", user.getEmail());
				pd.put("jurisdiction", user.getJurisdiction());
				pd.put("school", user.getSchool());
				pd.put("grade", user.getGrade());
				pd.put("img", user.getImg());
				pd.put("perSignature", user.getPersignature());
				pd.put("userandgroup",1);//用户
				return pd;
			}
		} 
		pd.put("code", "-1");
		pd.put("msg", "查找失败");
		return pd;
	}

}
